<?php

namespace Home\Model;

class ChatListsModel extends CommonModel
{
    public function msgtype($content, $channel)
    {
        switch ((int)$channel) {
            case 1:
                $content = '[图片]';
                break;
            case 2:
                $content = '[语音]';
                break;
            case 3:
                $content = '[视频]';
                break;
            case 6:
                $content = '[位置]';
                break;
            case 8:
                $content = '[礼物]';
                break;
            case 9:
                $content = '[语音通话]';
                break;
            case 10:
                $content = '[视频通话]';
                break;
            case 11:
            case 17:
                $content = '[表情]';
                break;
        }
        return $content;
    }
    // 更新会话列表
    public function updateLists($from_user, $to_user, $single = 0, $channel = 0, $content = '', $fate = 0, $sex='男')
    {
        $ChatListsModel = new ChatListsModel;

        $current_time = time();

        if ($channel !== 0) {
            $content = $this->msgtype($content, $channel);
        }

        $user_id = min($from_user, $to_user);
        $another_id = max($from_user, $to_user);

        $lists = $ChatListsModel->master(true)->where(['user_id' => $user_id, 'another_id' => $another_id])->find();

        if ($user_id == $from_user) { // 发送者ID比对方小
            $field_channel = 'another_channel';
            $field_unread = 'another_unread';
            $field_untip = 'another_untip';
            $field_content = 'another_content';
            $field_delete = 'another_delete';
            $field_time = 'another_time';
            $field_first = 'user_first';

            $field_content_both = 'user_content';
            $field_channel_both = 'user_channel';
            $field_time_both = 'user_time';
            $field_delete_both = 'user_delete';
            $field_first_both = 'another_first';
        } else {
            $field_channel = 'user_channel';
            $field_unread = 'user_unread';
            $field_untip = 'user_untip';
            $field_content = 'user_content';
            $field_delete = 'user_delete';
            $field_time = 'user_time';
            $field_first = 'another_first';

            $field_content_both = 'another_content';
            $field_channel_both = 'another_channel';
            $field_time_both = 'another_time';
            $field_delete_both = 'another_delete';
            $field_first_both = 'user_first';
        }

        if ($lists) {
            $update = [
                'id' => $lists['id'],
                $field_content => $content,
                $field_channel => $channel,
                $field_unread => $lists[$field_unread] + 1,
                $field_untip => $lists[$field_untip] + 1,
                $field_delete => 0,
                $field_delete_both => 0,
                $field_time => $current_time,
                //'another_first' => 0,
            ];

            if ($channel != -1) {
                $update[$field_first_both] = 0;
            }
            if ($fate == 3) {
                $update[$field_first] = 0;
            }

            // 非单边消息，发送方列表也更新
            if (!$single) {
                $update[$field_content_both] = $content;
                $update[$field_channel_both] = $channel;
                $update[$field_time_both] = $current_time;
            } else if ($single == 2) {
                // 仅发出方可见
                $update = ['id'=>$lists['id']];
                unset($update[$field_delete]);
                unset($update[$field_delete_both]);
                $update[$field_content_both] = $content;
                $update[$field_channel_both] = $channel;
                $update[$field_time_both] = $current_time;
            }

            $ChatListsModel->save($update);
        } else {
            $from = I('from', 1);

            $create = [
                'user_id' => $user_id,
                'another_id' => $another_id,
                $field_content => $content,
                $field_channel => $channel,
                $field_unread => 1,
                $field_untip => 1,
                $field_time => $current_time,
            ];

            if (($fate || $content != '我在这儿呢') && !in_array($fate, [2,3])) {
                $create[$field_first] = 1;
            }

            // 非单边消息，发送方列表也更新
            if (!$single) {
                $create[$field_content_both] = $content;
                $create[$field_channel_both] = $channel;
                $create[$field_time_both] = $current_time;
            } else if ($single == 2) {
                // 仅发出方可见
                $create = [
                    'user_id' => $user_id,
                    'another_id' => $another_id,
                    $field_content => $content,
                    $field_channel => $channel,
                    $field_unread => 0,
                    $field_untip => 0,
                    $field_time => $current_time,
                    $field_first=>$fate==2?0:1,
                    $field_delete=>1
                ];
                $create[$field_content_both] = $content;
                $create[$field_channel_both] = $channel;
                $create[$field_time_both] = $current_time;
            }

            $create['from'] = $from;

            $ChatListsModel->add($create);
        }
    }

    public function changeLists($from_user, $to_user, $field = 'unread', $status=0)
    {
        $ChatListsModel = new ChatListsModel;

        $user_id = min($from_user, $to_user);
        $another_id = max($from_user, $to_user);

        $lists = $ChatListsModel->where(['user_id' => $user_id, 'another_id' => $another_id])->find();

        if ($user_id == $from_user) {
            $field_change = 'user_';
        } else {
            $field_change = 'another_';
        }

        if ($lists) {
            if (in_array($field, ['delete_one', 'delete_all'])) {
                if ($field == 'delete_all') {
                    $field_another = $user_id == $from_user ? 'another_' : 'user_';
                    
                    if ($lists[$field_another . 'delete']) {
                        $ChatListsModel->delete($lists['id']);
                        return;
                    }
                    
                    $update = [
                        'id' => $lists['id'],
                        $field_change . 'content' => '',
                        $field_change . 'unread' => 0,
                        //$field_change . 'delete' => 1,
                    ];
                } else if ($field == 'delete_one') {
                    $Privatechat = new PrivatechatModel;

                    $where = [
                        'type' => ['neq', -1],
                        'single' => 0,
                        'userid' => $from_user,
                        'touserid' => $to_user,
                        'status' => ['in', [0,2]]
                    ];
                    $map['_complex'][] = $where;
                    $where = [
                        'type' => ['neq', -1],
                        'single' => 0,
                        'userid' => $to_user,
                        'touserid' => $from_user,
                        'status' => ['in', [0,1]]
                    ];
                    $map['_complex'][] = $where;

                    $map['_logic'] = 'or';

                    $newest = $Privatechat->where($map)->order('id desc')->find();

                    if($newest){
                        $newest['content'] = $this->msgtype($newest['content'], $newest['type']);
                    }
                    
                    $update = [
                        'id' => $lists['id'],
                        $field_change . 'content' => $newest ? $newest['content'] : '[消息已删除]',
                        $field_change . 'unread' => 0,
                    ];
                }
            } else if ($field == 'recall') {
                if ($user_id == $from_user) {
                    $field_change = 'another_';
                    $field_change_another = 'user_';
                } else {
                    $field_change = 'user_';
                    $field_change_another = 'another_';
                }

                $Privatechat = new PrivatechatModel;

                $where = [
                    'type' => ['neq', -1],
                    'single' => 0,
                    'userid' => $from_user,
                    'touserid' => $to_user,
                    'status' => ['in', [0,2]]
                ];
                $map['_complex'][] = $where;
                $where = [
                    'type' => ['neq', -1],
                    'single' => 0,
                    'userid' => $to_user,
                    'touserid' => $from_user,
                    'status' => ['in', [0,1]]
                ];
                $map['_complex'][] = $where;

                $map['_logic'] = 'or';

                $newest = $Privatechat->where($map)->order('id desc')->find();
                    
                if($newest){
                    $newest['content'] = $this->msgtype($newest['content'], $newest['type']);
                }

                $update = [
                    'id' => $lists['id'],
                    $field_change . 'content' => $newest ? $newest['content'] : '[消息已撤回]',
                    $field_change_another . 'content' => '[消息已撤回]',
                    $field_change . 'unread' => $lists[$field_change . 'unread'] ? $lists[$field_change . 'unread'] - 1 : 0,
                ];
            } else {
                $update = [
                    'id' => $lists['id'],
                    $field_change . $field => $status,
                ];

                // 删除列表也做已读
                if ($field == 'delete') {
                    $field_another = $user_id == $from_user ? 'another_' : 'user_';
                    if ($lists[$field_another . 'delete']) {
                        $ChatListsModel->delete($lists['id']);
                        return;
                    }
                    
                    $update[$field_change . 'unread'] = 0;
                }
            }

            $ChatListsModel->save($update);
        }
    }

    public function synchLists($userid, $data='')
    {
        $ChatListsModel = new ChatListsModel;
        $ChatListsRecordsModel = new ChatListsRecordsModel;

        if ($data) {
            // 有会话记录的用户
            $ids = $ChatListsModel->where(['user_id|another_id' => $userid])->getField('id, user_id, another_id');

            $uids = [];
            $lists_ids = [];
            foreach ($ids as $key => $item) {
                $uids[] = $item['user_id'] == $userid ? $item['another_id'] : $item['user_id'];
                $another_id = $item['user_id'] == $userid ? $item['another_id'] : $item['user_id'];
                $lists_ids[$another_id] = $key;
            }

            $create_user = [];
            $create_another = [];
            $update_user = [];
            $update_another = [];
            foreach ($data as $key => $item) {
                $touserid = $item['touserid'] != $userid ? $item['touserid'] : $item['userid'];

                $user_id = min($touserid, $userid);
                $another_id = max($touserid, $userid);

                if ($user_id == $userid) { // ID小
                    $field_channel = 'user_channel';
                    $field_unread = 'user_unread';
                    $field_content = 'user_content';
                    $field_delete = 'user_delete';
                    $field_time = 'user_time';
                    $field_top = 'user_top';
                } else {
                    $field_channel = 'another_channel';
                    $field_unread = 'another_unread';
                    $field_content = 'another_content';
                    $field_delete = 'another_delete';
                    $field_time = 'another_time';
                    $field_top = 'another_top';
                }

                if (in_array($touserid, $uids)) {
                    if ($user_id == $userid) {
                        $update_user[] = [
                            'id' => $lists_ids[$touserid],
                            $field_content => $item['content'],
                            $field_channel => $item['type'],
                            $field_unread => $item['count'],
                            $field_delete => 0,
                            $field_time => $item['sort_field'],
                            $field_top => $item['top'],
                        ];
                    } else {
                        $update_another[] = [
                            'id' => $lists_ids[$touserid],
                            $field_content => $item['content'],
                            $field_channel => $item['type'],
                            $field_unread => $item['count'],
                            $field_delete => 0,
                            $field_time => $item['sort_field'],
                            $field_top => $item['top'],
                        ];
                    }
                } else {
                    if ($user_id == $userid) {
                        $create_user[] = [
                            'user_id' => $user_id,
                            'another_id' => $another_id,
                            $field_content => $item['content'],
                            $field_channel => $item['type'],
                            $field_unread => $item['count'],
                            $field_time => $item['sort_field'],
                            $field_top => $item['top'],
                        ];
                    } else {
                        $create_another[] = [
                            'user_id' => $user_id,
                            'another_id' => $another_id,
                            $field_content => $item['content'],
                            $field_channel => $item['type'],
                            $field_unread => $item['count'],
                            $field_time => $item['sort_field'],
                            $field_top => $item['top'],
                        ];
                    }

                }
            }

            if ($create_user) {
                $ChatListsModel->addAll($create_user);
            }
            if ($create_another) {
                $ChatListsModel->addAll($create_another);
            }

            foreach ($update_user as $item) {
                $ChatListsModel->save($item);
            }
            foreach ($update_another as $item) {
                $ChatListsModel->save($item);
            }
        }

        $ChatListsRecordsModel->add(['user_id' => $userid]);
    }
}
